﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using bdnt_pets_system_Entity;
using System.Windows.Data;
using bdnt_pets_system.Tools;
using bdnt_pets_system.Resources;

namespace bdnt_pets_system.PageViews.DataManager
{
    public partial class NewStationWindow : BaseWindow
    {
        DataServiceReference.DataServiceClient dataClient;
        public NewStationWindow(Tbl_Station cuTbl_Station)
        {
            InitializeComponent();
            if (cuTbl_Station.Id == 0)
                this.Title ="新建工位";
            else this.Title = "修改工位";

            dataClient = new DataServiceReference.DataServiceClient();
            CuTbl_Station = cuTbl_Station;
            BingData();
            InitEvnet();
        }

        public Tbl_Station CuTbl_Station
        {
            get { return this.DataContext as Tbl_Station; }
            set { this.DataContext = value; }
        }

        void BingData()
        {
            tbStationNo.SetBinding(TextBox.TextProperty, new Binding("StationNo")
            {
                Mode = BindingMode.TwoWay,
                NotifyOnValidationError = true,
                ValidatesOnExceptions = true
            });

            tbStationName.SetBinding(TextBox.TextProperty, new Binding("StationName")
            {
                Mode = BindingMode.TwoWay,
                NotifyOnValidationError = true,
                ValidatesOnExceptions = true
            });
            tben_StationName.SetBinding(TextBox.TextProperty, new Binding("en_StationName")
            {
                Mode = BindingMode.TwoWay,
                NotifyOnValidationError = true,
                ValidatesOnExceptions = true
            });
            tbRemark.SetBinding(TextBox.TextProperty, new Binding("Remark") { Mode = BindingMode.TwoWay });
        }

        /// <summary>
        /// 字段验证方法
        /// </summary>
        void BindgExpression()
        {
            tbStationNo.GetBindingExpression(TextBox.TextProperty).UpdateSource();
            tbStationName.GetBindingExpression(TextBox.TextProperty).UpdateSource();
        }

        void InitEvnet()
        {
            btnOk.Click += new RoutedEventHandler(btnOk_Click);
            btnCancel.Click += new RoutedEventHandler(btnCancel_Click);
            dataClient.SaveStationCompleted += new EventHandler<DataServiceReference.SaveStationCompletedEventArgs>(dataClient_SaveStationCompleted);
            dataClient.GetShopListByConditionCompleted += new EventHandler<DataServiceReference.GetShopListByConditionCompletedEventArgs>(dataClient_GetShopListByConditionCompleted);
            InitData();
        }

        void InitData()
        {
            dataClient.GetShopListByConditionAsync(null,1000,1);
        }

        void dataClient_GetShopListByConditionCompleted(object sender, DataServiceReference.GetShopListByConditionCompletedEventArgs e)
        {
            var cuSts = e.Result;
            cbShopName.ItemsSource = cuSts;
            if (SessionManager.IsCN_ZH)
                cbShopName.DisplayMemberPath = "ShopName";
            else
                cbShopName.DisplayMemberPath = "en_ShopName";
            cbShopName.SelectedValuePath = "ShopNo";
            if (!string.IsNullOrEmpty(CuTbl_Station.ShopNo))
            {
                var sh = cuSts.FirstOrDefault(p => p.ShopNo == CuTbl_Station.ShopNo);
                if (sh == null)
                    cbShopName.SelectedIndex = -1;
                cbShopName.SelectedIndex = cuSts.IndexOf(sh);
            }
            
        }

        void dataClient_SaveStationCompleted(object sender, DataServiceReference.SaveStationCompletedEventArgs e)
        {
            if (e.Result)
                this.DialogResult = true;
            else
                errorMsg.SetBinding(TextBlock.TextProperty, new Binding("Strings.bcsb") { Source = CuLc });
        }

        void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        void btnOk_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                BindgExpression();
                if (vs.Errors.Count > 0)
                {
                    errorMsg.Text = MyStrings.sjtxbq;
                    return;
                }
                if (cbShopName.SelectedValue != null)
                    CuTbl_Station.ShopNo = cbShopName.SelectedValue.ToString();
                dataClient.SaveStationAsync(CuTbl_Station);
            }
            catch (Exception)
            {

            }
        }

    }
}

